/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 08 - 01
 * Time:17:11
 */

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import models.UserInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/***
 * 1.Jackson的使用
 */
public class Test {
    public static void main(String[] args) throws JsonProcessingException {
       ObjectMapper mapper = new ObjectMapper();
/*         //{"succ" : 1, "msg": "msg"}
        HashMap<String ,Object> map = new HashMap<>();
        map.put("succ",1);
        map.put("msg","操作结果");
        //object —— json String
        String str = mapper.writeValueAsString(map);
        System.out.println(str);*/

        //将列表转换为标准的json字符串格式
        List<UserInfo> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername("java" + i);
            userInfo.setPassword("pwd" + i);
            list.add(userInfo);
        }
        String  ret = mapper.writeValueAsString(list);
        System.out.println(ret);

        /*
        * [{"id":0,"createtime":null,"updatetime":null,"username":"java0","password":"pwd0","state":0},
        * {"id":0,"createtime":null,"updatetime":null,"username":"java1","password":"pwd1","state":0},
        * {"id":0,"createtime":null,"updatetime":null,"username":"java2","password":"pwd2","state":0}]
         */
    }
}
